package com.sz.controller;

import com.sz.pojo.Plan;
import com.sz.pojo.RevExp;
import com.sz.pojo.User;
import com.sz.service.PlanSVImpl;
import com.sz.service.RevExpSVImpl;
import com.sz.service.UserSVImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {
    @Resource
    private UserSVImpl userSV;
    @Resource
    private PlanSVImpl planSV;
    @Resource
    private RevExpSVImpl revExpSV;

    @RequestMapping("/toIndex")
    public String index(HttpServletRequest req, Model model){
        Object session_auth = req.getSession().getAttribute("auth");
        if (session_auth!=null){
            int auth = (Integer)session_auth;
            if (auth==0){
                return "index10";
            }else if(auth==1){
                return "index11";
            }else{
                return "index12";
            }
        }else{
            return "index12";
        }
    }

    @RequestMapping("/toIndex2")
    public String index2(Model model){
        //回显计划
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String year = sdf.format(date);
        Plan thisYearPlan = planSV.getPlanById(Integer.valueOf(year));
        model.addAttribute("thisYearPlan",thisYearPlan);
        //回显添加者
        List<RevExp> allAdder = revExpSV.getAllAdder();
        model.addAttribute("allAdder",allAdder);
        return "index2";
    }

    @RequestMapping("/toIndex3")
    public String index3(Model model,HttpServletRequest req){
        model.addAttribute("authority","y");
        int auth = (Integer) req.getSession().getAttribute("auth");
        List<User> lowUser = userSV.getAllByAuth(auth);
        model.addAttribute("lowUser",lowUser);
        return "index3";
    }

    @RequestMapping("/noauth")
    public String noauth(Model model){
        model.addAttribute("authority","n");
        return "forward:/query/all2";
    }
}
